const path = require('path');

const HtmlWebPackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
//用于在构建前清除dist目录中的内容
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

const webpack = require('webpack');

var CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    mode: "development",

    entry: './index.js', // 入口文件
    output: {
        path: path.resolve(__dirname, 'dist'), // 定义输出目录
        //filename: 'index.js'  // 定义输出文件名称
        filename: '[name].js',  // 定义输出文件名称
        chunkFilename: '[name].chunk.js'
    },
    resolve: {
        extensions: [".js", ".json", ".jsx", ".css", ".scss"],
        alias: {
            '@src': path.resolve(__dirname, './src/')
        }
    },
    module: {
        rules: [
            {
                test: require.resolve('zepto'),
                use: ['exports-loader?window.Zepto', 'script-loader']
            },
            {
                test: /\.(js|jsx)$/, // 匹配.js文件
                exclude: /node_modules/,
                use: {
                    loader: 'babel-loader'
                }
            },
            {
                test: /\.css$/,
                use: [{
                    loader: "style-loader" // 将 JS 字符串生成为 style 节点
                }, {
                    loader: "css-loader" // 将 JS 字符串生成为 style 节点
                }]

            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "style-loader" // 将 JS 字符串生成为 style 节点
                    },
                    {
                        loader: "css-loader" // 将 CSS 转化成 CommonJS 模块
                    },
                    {
                        loader: "sass-loader" // 将 Sass 编译成 CSS
                    }
                ]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [
                    {
                        loader: 'url-loader'
                    }
                ]
            },
            {
                test: /\.(eot|svg|ttf|woff|woff2|otf)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 8192,
                            name: 'resource/[name].[ext]'
                        }
                    }
                ]
            }
        ]
    },
    devServer: {
        hot: true, // 热替换
        contentBase: path.join(__dirname, 'dist'), // server文件的根目录
        compress: true, // 开启gzip
        host: 'localhost', //  192.168.0.100
        port: 8083, // 端口
        proxy: {
            '/teach_api/': {
                target: 'http://101.132.120.193:1004/',
                // target: 'http://localhost:60272/',
                pathRewrite: { '^/teach_api': 'api' },
                changeOrigin: true,     // target是域名的话，需要这个参数，
                secure: false,          // 设置支持https协议的代理
            },
            '/ng_images/': {
                target: 'http://101.132.120.193:1005/',
                // target: 'http://localhost:60272/',
                pathRewrite: { '^/ng_images': 'ng_images' },
                changeOrigin: true,     // target是域名的话，需要这个参数，
                secure: false,          // 设置支持https协议的代理
            }
        }


    },
    plugins: [
        // new webpack.NoErrorsPlugin(), //允许错误不打断程序
        new CleanWebpackPlugin(),
        new webpack.HotModuleReplacementPlugin(), // HMR允许在运行时更新各种模块，而无需进行完全刷新
        new HtmlWebPackPlugin({
            template: './index.html',
            filename: path.resolve(__dirname, 'dist/index.html')
        }),
        new HtmlWebPackPlugin({
            template: './baoli.html',
            filename: path.resolve(__dirname, 'dist/baoli.html')
        }),
        new CopyWebpackPlugin([
            {
                from: './pic',
                to: './pic'
            }
        ]),
        new CopyWebpackPlugin([
            {
                from: './public',
                to: './public'
            }
        ])
    ]
};